package com.yzh.booksystem.controller;

import com.yzh.booksystem.entity.Book;
import com.yzh.booksystem.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "CheckServlet",urlPatterns = "/CheckServlet")
public class CheckServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String bookname = request.getParameter("bookname");
        Book book = new Book();
        book.setBookname(bookname);
        UserService userService = new UserService();
        //查找单个图书的流程控制
        try {
            if (userService.single_book_show(book) != null) {
                request.setAttribute("book", userService.single_book_show(book));
                PrintWriter writer = response.getWriter();
                request.getRequestDispatcher("/findresult.jsp").forward(request,response);
                //writer.println("查找成功,3s后挑战到成功页");
//                response.setHeader("refresh", "3;url=/booksystem/findresult.jsp");
                //request.getRequestDispacher().forward()
                //response.sendRedirect();
                //response.setHeader("refresh", "3;url=");
//                request.getRequestDispatcher("/findresult.jsp").forward(request, response);
            } else {
                //第二种添加错误信息的方式在这里：
                request.setAttribute("error2", "抱歉，数据库中没有这本书，请重新查找");
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
